﻿using System;
using System.IO;
using System.Windows.Forms;

namespace PictureResizerForms
{
    class GlobalSettings
    {
        private static dataTableBasedSettings _Templates;
        private String _XMLTemplatesPath;
        private static bool _flag_XML_Loaded = false;

        public GlobalSettings()
        {
            //Objekt instanzieren
            if (File.Exists(Properties.Settings.Default.TemplateFilePath))
            {
                this.init();
            }else
            {
                this.createConfigFile();
            }
        }

        public void init()
        {
            _XMLTemplatesPath = Properties.Settings.Default.TemplateFilePath;
            if (_Templates == null) _Templates = new dataTableBasedSettings();
            if (!_flag_XML_Loaded) this.Load();
        }

        private void createConfigFile()
        {
            if (!File.Exists(Properties.Settings.Default.TemplateFilePath))
            {
                dataTableBasedSettings initDT = new dataTableBasedSettings();
                initDT.dt.WriteXml(Properties.Settings.Default.TemplateFilePath);
            }

            this.init();
        }

        private void Load()
        {
            if (System.IO.File.Exists(_XMLTemplatesPath))
            {
                _Templates.Load(_XMLTemplatesPath);
                _flag_XML_Loaded = true;
            }
        }

        /// <summary>
        /// Speichert die Templates
        /// </summary>
        public void Save()
        {
            _Templates.Save(_XMLTemplatesPath);
        } 

        public dataTableBasedSettings Templates()
        {
            return _Templates;
        }
    }
}
